#include"Log.hpp"
#include"Reactor.hpp"
#include"Listener.hpp"
#include"HandlerConnection.hpp"
#include"PackageParse.hpp"

#include<iostream>
#include<memory>



int main(int argc,char *argv[])
{
    if(argc!=2)
    {
        std::cerr<<"Usage:"<<argv[0]<<" serverport"<<std::endl;
    }
    uint16_t port=std::stoi(argv[1]);
    InetAddr localaddr("0.0.0.0",port);

    Listener listen(port);//处理链接模块

    PackageParse package;
    HandlerConnection handler(std::bind(&PackageParse::Excute,&package,std::placeholders::_1));//普通sockfd-IO处理

    std::unique_ptr<Reactor> R=std::make_unique<Reactor>();//事件派发器

    //将各模块关联起来
    R->SetConnectHandler(std::bind(&Listener::Accepter,&listen,std::placeholders::_1));
    R->SetNormalHandler(
        std::bind(&HandlerConnection::HandlerRecver,&handler,std::placeholders::_1),
        std::bind(&HandlerConnection::HandlerSender,&handler,std::placeholders::_1),
        std::bind(&HandlerConnection::HandlerExcepter,&handler,std::placeholders::_1)
    );
    R->AddConnection(listen.ListenSocket(),EPOLLIN|EPOLLET,localaddr,ListenConnection);
    // R->AddConnection(listen.ListenSocket(),EPOLLIN|EPOLLET,
    //     std::bind(&Listener::Accepter,&listen,std::placeholders::_1),nullptr,nullptr);
    R->Dispatcher();
    return 0;
}